#include "GamePlayLayer.h"
#include "Utils.h"
#include "Terrain.h"
#include "Bird.h"
#include "Column.h"
#include "SimpleAudioEngine.h"

GamePlayLayer::GamePlayLayer()
{

}

GamePlayLayer::~GamePlayLayer()
{
}

bool GamePlayLayer::init(){
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	scheduleUpdate();
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,2,true);

	hintSprite = 0;

	backgroundSprite = CCSprite::createWithSpriteFrameName("SceneBackgroundLight.png");
	backgroundSprite->setScale(1.2f);
	backgroundSprite->setPosition(ccp(visibleSize.width/2, visibleSize.height/2 + 40));
	addChild(backgroundSprite);

//#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//	Utils::gameServicesSignIn();
//#endif

	terrain = Terrain::create();
	terrain->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));
	addChild(terrain);

	int topPadding = 80;

	scoreLabel = CCLabelTTF::create("","", 18);
	scoreLabel->setPosition(ccp(80, visibleSize.height - topPadding));
	addChild(scoreLabel);

	highScoreLabel = CCLabelTTF::create("","", 18);
	highScoreLabel->setPosition(ccp(visibleSize.width - 100, visibleSize.height - topPadding));
	addChild(highScoreLabel);

	showMenu(MT_START);

	return true;
}

void GamePlayLayer::update(float delta){
	char str[32] = {0};
	
	sprintf(str, "Score: %d", GameManager::getInstance()->score);
	scoreLabel->setString(str);

	sprintf(str, "High score: %d", GameManager::getInstance()->highScore);
	highScoreLabel->setString(str);
}

bool GamePlayLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
	return true;
}

void GamePlayLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){
	if(state == GS_PLAY){
		terrain->touch();
		if(hintSprite){
			hintSprite->removeFromParent();
			hintSprite = 0;
		}
	}
}


void GamePlayLayer::draw(){
}

void GamePlayLayer::showMenu(int menuType){
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

	int leftPadding = 30;
	int buttonWidth = 104;
	
	replayButton = CCControlButton::create();
	replayButton->setBackgroundSpriteFrameForState(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("PlayButton.png"), CCControlStateNormal);
	replayButton->setAdjustBackgroundImage(false);
	replayButton->setPosition(ccp(leftPadding + replayButton->boundingBox().size.width/2, visibleSize.height/2 - 60));
	replayButton->addTargetWithActionForControlEvents(this, cccontrol_selector(GamePlayLayer::replayButtonTouched), CCControlEventTouchUpInside);
	addChild(replayButton);

	highScoreButton = CCControlButton::create();
	highScoreButton->setBackgroundSpriteFrameForState(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("HighscoreButton.png"), CCControlStateNormal);
	highScoreButton->setAdjustBackgroundImage(false);
	highScoreButton->setPosition(ccp(visibleSize.width - leftPadding - replayButton->boundingBox().size.width/2, replayButton->getPositionY()));
	highScoreButton->addTargetWithActionForControlEvents(this, cccontrol_selector(GamePlayLayer::highScoreButtonTouched), CCControlEventTouchUpInside);
	addChild(highScoreButton);

	voteButton = CCControlButton::create();
	voteButton->setBackgroundSpriteFrameForState(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("RateButton.png"), CCControlStateNormal);
	voteButton->setAdjustBackgroundImage(false);
	voteButton->setPosition(ccp(visibleSize.width/2, replayButton->getPositionY()  + 60));
	voteButton->addTargetWithActionForControlEvents(this, cccontrol_selector(GamePlayLayer::voteButtonTouched), CCControlEventTouchUpInside);
	addChild(voteButton);

	if(menuType == MT_GAMEOVER)
		gameOverSprite = CCSprite::createWithSpriteFrameName("GameOverText.png");
	else if(menuType == MT_START){
		setState(GS_MENU_START);
		gameOverSprite = CCSprite::createWithSpriteFrameName("FlappyText.png");
	}
	gameOverSprite->setPosition(ccp(visibleSize.width/2, replayButton->getPositionY() + 140));
	addChild(gameOverSprite);
}

void GamePlayLayer::replayButtonTouched(CCObject* sender, CCControlEvent event){
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

	terrain->resetGame();
	replayButton->removeFromParent();
	highScoreButton->removeFromParent();
	gameOverSprite->removeFromParent();
	voteButton->removeFromParent();
	setState(GS_PLAY);

	hintSprite = CCSprite::createWithSpriteFrameName("TapHint.png");
	hintSprite->setPosition(ccp(visibleSize.width/2, visibleSize.height/2 + 80));
	addChild(hintSprite);

	Utils::playEffect("Swooshing");
}

void GamePlayLayer::highScoreButtonTouched(CCObject* sender, CCControlEvent event){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	Utils::showLeaderboards();
#endif
}

void GamePlayLayer::voteButtonTouched(CCObject* sender, CCControlEvent event){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	Utils::rateApp();
#endif
}

void GamePlayLayer::setState(int state){
	this->state = state;
}

void GamePlayLayer::shake(){
	CCMoveBy* moveLeftAction = CCMoveBy::create(0.1f, ccp(-4, 0));
	CCMoveBy* moveRightAction = CCMoveBy::create(0.1f, ccp(6, 0));
	CCMoveBy* moveLeft2Action = CCMoveBy::create(0.1f, ccp(-2, 0));

	runAction(CCSequence::create(moveLeftAction, moveRightAction, moveLeft2Action, 0));
}